/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servicecategorization;

import java.util.List;
import java.util.Map;
import java.util.Vector;
import javax.wsdl.Definition;
import javax.wsdl.Operation;
import javax.wsdl.PortType;
import javax.wsdl.WSDLException;
import javax.wsdl.factory.WSDLFactory;
import javax.wsdl.xml.WSDLReader;
import javax.xml.namespace.QName;

/**
 *
 * @author Administrator
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static Vector<ServiceTuple> vServiceTuples;
    public static void main(String[] args) {
        try {
            ReadWSDL("http://ws.srlink.com/geoservices/globalweatherservice.asmx?wsdl");
        } catch (WSDLException e) {
            e.printStackTrace();
        }
        String[] terms;
        for(ServiceTuple st : vServiceTuples){
            String op = st.getOperation_name();
             terms = serviceTokenize(op);
             for(String s : terms){
                System.out.print(s+" ");
             }
             System.out.println();
        }
    }

    public static void ReadWSDL(String wsdl_url) throws WSDLException {
        WSDLFactory factory = WSDLFactory.newInstance();
        WSDLReader reader = factory.newWSDLReader();
        reader.setFeature("javax.wsdl.verbose", false);
        reader.setFeature("javax.wsdl.importDocuments", true);

        Definition def = reader.readWSDL(wsdl_url);        //<-- the <description> element
        vServiceTuples = new Vector<ServiceTuple>();


        Map portTypes = def.getPortTypes();
        for (Object key : portTypes.keySet()) {
            PortType portType = def.getPortType((QName) key);
            //System.out.println("Porttype QName ->" + semanticPortType.getQName());

            List operations = portType.getOperations();
            for (Object operation : operations) {
                ServiceTuple st = new ServiceTuple();
                String temp = "";

                temp = ((Operation) operation).getName();
                st.setOperation_name(temp);

                temp = ((Operation) operation).getInput().getMessage().getParts().get("parameters").toString();
                temp = temp.substring(temp.lastIndexOf("}") + 1);
                st.setInput_name(temp);

                temp = (String) ((Operation) operation).getOutput().getMessage().getParts().get("parameters").toString();
                temp = temp.substring(temp.lastIndexOf("}") + 1);
                st.setOutput_name(temp);

                try {
                    st.setDocumentation(((Operation) operation).getDocumentationElement().getTextContent());
                } catch (java.lang.NullPointerException npe) {
                    npe.printStackTrace();
                }
//                System.out.println();
//                System.out.println(st);
                vServiceTuples.add(st);
            }
        }
    }

    /**
     * This takes a came case string such as "GetParticipantsByRoutingNumberResult"
     * and breaks it up into its word pairs depending on Its capical letters.
     * If there is a series of capical letters such as "StudentFromMIT" or
     * "ValidateIBANResult" It will group all the capital letters except for
     * the last one.  
     * @param string
     * @return
     */
    public static String[] serviceTokenize(String string) {
        String[] rS = {""};
        StringBuilder sb = new StringBuilder();
        Vector<String> vs = new Vector<String>();
        
        if(string ==null)
            return rS;

        boolean lastUpperCase = false;

        Character c = ' ';
        Character last_c = ' ';
        for (int i = 0; i < string.length(); ++i) {
            c = string.charAt(i);
            if (Character.isUpperCase(c) && !lastUpperCase) {
                if (sb.length() > 0) {
                    vs.add(sb.toString());
                    sb.delete(0, sb.length());
                    sb.trimToSize();
                }
                sb.append(c);
                lastUpperCase = true;
            } else if (Character.isUpperCase(c) && lastUpperCase) {
                sb.append(string.charAt(i));
            } else if (!Character.isUpperCase(c) && lastUpperCase) {
                sb.deleteCharAt(sb.lastIndexOf(last_c.toString()));
                vs.add(sb.toString());
                sb.delete(0, sb.length());
                sb.trimToSize();
                sb.append(last_c);
                sb.append(c);
                lastUpperCase = false;
            } else {
                sb.append(c);
                lastUpperCase = false;
            }
            last_c = c;
        }
        vs.add(sb.toString());
        while (vs.remove("")) {
            ;
        }
        return vs.toArray(rS);
    }

    public static void insertSynonyms() {
    }
}
